package com.easy.auth.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easy.auth.domain.Role;
import com.easy.auth.service.ActionService;
import com.easy.auth.service.RoleService;
import com.easy.common.jdbc.PageInfo;
import com.easy.common.web.FilterUtil;
import com.easy.common.web.PagedList;
import com.easy.common.web.message.DeleteMessage;
import com.easy.common.web.message.Message;
import com.easy.common.web.message.SaveOrEditMessage;

@Controller
@RequestMapping("RoleController")
public class RoleController {

	@Autowired
	private RoleService roleService;
	@Autowired
	private ActionService actionService;

	/**
	 * 跳转到角色管理页面
	 * 
	 * @return
	 */
	@RequestMapping("goToRoleManager")
	public String goToRoleManager(String secondMenuId,HttpServletRequest request) {
		request.getSession().setAttribute("secondMenuId", secondMenuId);
		return "auth/role";
	}

	/**
	 * 获取角色列表
	 * 
	 * @return
	 */
	@RequestMapping("getRoleList")
	@ResponseBody
	public PagedList<Role> getRoleList(PageInfo pageInfo) {
		Role role = (Role) FilterUtil.getVO(Role.class,
				pageInfo.getFilterRules());
		return roleService.getAllRoles(role, pageInfo);
	}

	/**
	 * 编辑角色时查询选择的角色信息
	 * 
	 * @param role
	 * @return
	 */
	@RequestMapping("findRole")
	@ResponseBody
	public Role findRole(Role role) {
		return roleService.findRole(role);
	}

	/**
	 * 保存角色信息
	 * 
	 * @param role
	 * @return
	 */
	@RequestMapping("saveRoleInfo")
	@ResponseBody
	public Message saveRoleInfo(Role role) {
		return new SaveOrEditMessage(roleService.saveOrEditRole(role));
	}

	/**
	 * 删除选中的角色
	 * 
	 * @param oids
	 *            删除角色的id集合
	 * @return
	 */
	@RequestMapping("removeRole")
	@ResponseBody
	public Message removeRole(String oids) {
		return new DeleteMessage(roleService.removeRoles(oids));
	}

	/**
	 * 获取用户已有角色列表
	 * 
	 * @param userid
	 * @param pageInfo
	 * @return
	 */
	@RequestMapping("getUserHaveRoleList")
	@ResponseBody
	public PagedList<Role> getUserHaveRoleList(String userid, PageInfo pageInfo) {

		return roleService.getRolesByUserId(userid, pageInfo);
	}

	/**
	 * 获取用户未分配角色列表
	 * 
	 * @param userid
	 * @param pageInfo
	 * @return
	 */
	@RequestMapping("getUserNotHaveRoleList")
	@ResponseBody
	public PagedList<Role> getUserNotHaveRoleList(String userid,
			PageInfo pageInfo) {

		return roleService.getNotHaveRolesByUserId(userid, pageInfo);
	}

	/**
	 * 移除用户已有角色
	 * 
	 * @param userid
	 * @param roleids
	 * @return
	 */
	@RequestMapping("removeUserHaveRole")
	@ResponseBody
	public DeleteMessage removeUserHaveRole(String userid, String roleids) {

		return new DeleteMessage(roleService.removeRoles(userid,
				roleids.split("-")));
	}

	/**
	 * 增加用户未分配角色
	 * 
	 * @param userid
	 * @param roleids
	 * @return
	 */
	@RequestMapping("addUserNotHaveRole")
	@ResponseBody
	public SaveOrEditMessage addUserNotHaveRole(String userid, String roleids) {

		return new SaveOrEditMessage(roleService.addRoles(userid,
				roleids.split("-")));
	}
}
